﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ServiceModel;
using System.Windows.Data;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Regions;
using YunBox2.Client.Infrastructure.ViewModels;
using YunBox2.Contracts.FoundationModels;
using YunBox2.Contracts;

namespace YunBox2.Client.Foundation.ViewModels
{
    [Export]
    public class UOMViewModel : MainContentViewModel, IConfirmNavigationRequest
    {
        [Import]
        private IRegionManager _regionManager;


        public UOMViewModel()
        {
            LoadAllUom();
            _UOMs = new ObservableCollection<UOM>(_UOMList);
            UOMCollectionView = new ListCollectionView(_UOMs);
            UOMCollectionView.CurrentChanged += (s, e) =>
                                                    {
                                                        var cv = (ICollectionView) s;
                                                        var uom = cv.CurrentItem as UOM;
                                                        if (uom == null)
                                                        {
                                                            Load(Guid.Empty);
                                                        }
                                                        else
                                                        {
                                                            Load(uom.Id.HasValue ? uom.Id.Value : Guid.Empty);
                                                        }
                                                    };
            // UOMCollectionView.MoveCurrentTo(null);
        }


        public override string Title
        {
            get
            {
                return "计量单位管理";
            }
        }

        #region IConfirmNavigationRequest 成员

        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
        }

        #endregion

        #region INavigationAware 成员

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            string strId = navigationContext.Parameters["id"];
            if (!string.IsNullOrWhiteSpace(strId))
            {
                Guid id;
                if (Guid.TryParse(strId, out id))
                {
                    Load(id);
                }
            }
        }

        #endregion

        private void Load(Guid id)
        {
            var query = new UriQuery();
            query.Add("id", id.ToString("D"));
            //ScopeRegionManager.RequestNavigate("UOMEditor", "UOMEditorView" + query);
            _regionManager.RequestNavigate("UOMEditor", "UOMEditorView" + query.ToString());
        }

        #region UOM列表

        public readonly ObservableCollection<UOM> _UOMs;
        private IList<UOM> _UOMList;

        public ICollectionView UOMCollectionView
        {
            get;
            set;
        }

        private void LoadAllUom()
        {
            var foundationService = SessionContext.FoundationService;

            using (OperationContextScope cs = SessionContext.CreateOperationContextScope(foundationService as IContextChannel))
            {
                _UOMList = SessionContext.FoundationService.GetAllUOM();
            }
        }

        #endregion
    }
}